package com.mingyizhudao.app.view.recommend.adapter;

import java.util.ArrayList;
import java.util.List;

import com.mingyizhudao.app.R;
import com.mingyizhudao.app.entity.HospitalEntity;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.utils.Res;
import com.mingyizhudao.app.view.recommend.DeptIntroduceActivity;
import com.mingyizhudao.app.view.recommend.bean.MenuItem;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HospitalDeptAdapter extends BaseAdapter
{
	private List<MenuItem> mData;
	private Context context;
	private String deptUrl;
	private HospitalEntity hospitalEntity;
	LinearLayout itemLayout;
	WindowManager wm ;
	int widthWindowSystem ;
	private class CategoryItem
	{
		public TextView dept_tv;
		public ImageView dept_iv;
		public LinearLayout ll_container;
		
	}

	public HospitalDeptAdapter(HospitalEntity hospitalEntity,String deptUrl,List<MenuItem> mData, Context context )
	{
		// TODO Auto-generated constructor stub
		this.hospitalEntity = hospitalEntity;
		this.mData = mData;
		this.context = context;
		this.deptUrl = deptUrl;
		Res.init( context );
		this.wm = ( WindowManager )context.getSystemService(Context.WINDOW_SERVICE);
		this.widthWindowSystem = wm.getDefaultDisplay().getWidth();
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mData==null?0:mData.size();
	}

	@Override
	public Object getItem( int position )
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId( int position )
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView( final int position, View convertView, ViewGroup parent )
	{
		// TODO Auto-generated method stub
		final CategoryItem item;
			convertView = View.inflate( context, R.layout.hospital_dept_listitem, null );
			
			item = new CategoryItem();
			item.dept_tv = ( TextView )convertView.findViewById( R.id.dept_tv );
			item.dept_iv = ( ImageView )convertView.findViewById( R.id.dept_iv );
			item.ll_container = ( LinearLayout )convertView.findViewById( R.id.ll_container );
			
		
		if( mData.get( position )!=null )
		{
			item.dept_tv.setText( mData.get( position ).getName() );
			item.dept_iv.setImageResource( Res.getDrawableID( mData.get( position ).getIconName() ) );
			
			ArrayList<MenuItem> childMenuItems = mData.get( position ).getChildMenuItems();
			
			setupTagViews(item.ll_container , childMenuItems);
		}
		
		return convertView;
	}
	 private void setupTagViews(LinearLayout ll_container ,final ArrayList<MenuItem> childMenuItems) {
         int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
         int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

         itemLayout = new LinearLayout(context);
         itemLayout.setOrientation(LinearLayout.HORIZONTAL);
         itemLayout.setLayoutParams(getTagParams());
         ll_container.addView(itemLayout);
         int currentRow = 0;
         for (int i = 0; i < childMenuItems.size(); i++) {
        	 final MenuItem childMenuItem = childMenuItems.get(i);
             TextView textView = new TextView(context);
              
              textView.setTextColor(context.getResources().getColor(R.color.light_black));
              textView.setBackgroundResource(R.drawable.dept_selector_bg);
              textView.setTextSize(14f);
              textView.setGravity(Gravity.CENTER);
              textView.setLayoutParams(getLableTextParms());
              textView.setPadding(20, 10, 20, 10);
              textView.setFocusable( false );
              textView.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                	String deptId = childMenuItem.getId();   
                	String url = deptUrl+ deptId;
   					String facultyName = childMenuItem.getName();
   					Bundle bundle = new Bundle();
   					
   					bundle.putSerializable( "deptUrl", url );
   					
   					bundle.putSerializable("deptId", deptId);
   					bundle.putSerializable("facultyName", facultyName );
   					
   					bundle.putSerializable("hospitalName", hospitalEntity.getName());
   					IntentHelper.getInstance(context).gotoActivity(DeptIntroduceActivity.class, bundle);
                   }
              });
              textView.setText(childMenuItem.getName().trim());
              currentRow = measureTagView(textView, w, h, currentRow,ll_container);
         }
    }
	 
	 
//   计算View尺寸,返回当前行数
     private int measureTagView(TextView tagView,int width, int height, int currentRow,LinearLayout ll_container) {
          int windowWidth = widthWindowSystem;
          tagView.measure(width, height);
          itemLayout.measure(width, height);
          int layoutWidth = itemLayout.getMeasuredWidth();
          int tvWidth = tagView.getMeasuredWidth();
          if((layoutWidth+tvWidth) > (windowWidth - 100)){
               itemLayout = new LinearLayout(context);
               itemLayout.setOrientation(LinearLayout.HORIZONTAL);
               itemLayout.setLayoutParams(getTagParams());
               ll_container.addView(itemLayout);
               currentRow ++;
          }
          itemLayout.addView(tagView);

          return currentRow;
     }
	 private LinearLayout.LayoutParams getTagParams() {
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
         params.setMargins(20, 40, 20, 10);
         return params;
    }

    private LinearLayout.LayoutParams getLableTextParms() {
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
         params.setMargins(10, 0, 10, 0);
         return params;
    }
    
   
}
